package com.zhu.shopcenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.commons.utils.JwtInfo;
import com.zhu.commons.utils.JwtUtils;
import com.zhu.commons.utils.R;
import com.zhu.shopcenter.pojo.TradePayLog;
import com.zhu.shopcenter.service.ITradeOrderService;
import com.zhu.shopcenter.service.ITradePayLogService;
import com.zhu.shopcenter.vo.PayLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-08-19
 */
@RestController
@RequestMapping("api/auth/trade/pay/log")
@Api(tags = "交易历史记录")
public class TradePayLogController {
   @Autowired
    private ITradeOrderService tradeOrderService;
   @Autowired
    private ITradePayLogService tradePayLogService;
   @PostMapping("/addLog")
   @ApiOperation(value = "添加交易记录")
   public R addPayHistory(HttpServletRequest request,@RequestBody PayLogVo payLogVo){
       JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(request);
       String id = jwtInfo.getId();
       if(id==null){
           return R.error().message("请登录");
       }
       String orderNo = payLogVo.getOrderNo();
       LambdaQueryWrapper<TradePayLog> queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(TradePayLog::getOrderNo,orderNo);
       TradePayLog one = tradePayLogService.getOne(queryWrapper);
       if(one!=null){
           return R.ok().message("");
       }
       Boolean result= tradePayLogService.addPayHistory(id,payLogVo);
       if(result){
           return R.ok().message("添加成功！");
       }
       return R.error().message("添加失败");
   }
}
